<?php


namespace app\admin\controller\mall;
use app\admin\model\MallGoods;
use app\admin\model\MallOrder;
use app\admin\model\MallTradeRecordLog;
use app\admin\model\MallUser;
use app\admin\model\MallWithdrawRecord;
use app\admin\model\SystemAdmin;
use app\api\help\SendMsg;
use app\common\controller\AdminController;
use EasyAdmin\annotation\ControllerAnnotation;
use EasyAdmin\annotation\NodeAnotation;
use think\App;
use think\facade\Db;
/**
 * @ControllerAnnotation(title="提现待打款")
 */

class AuditStatus1 extends AdminController
{
    public function __construct(App $app)
    {
        parent::__construct($app);
        $this->model = new MallWithdrawRecord();
    }
    /**
     * @NodeAnotation(title="审核通过待打款")
     */
    public function wait_pay()
    {
        if ($this->request->isAjax()) {
            if (input('selectFields')) {
                return $this->selectList();
            }
            [$page, $limit, $where] = $this->buildTableParames();
            $where[]   = ['ea_mall_withdraw_record.status', 'in', [1]];
            $count     = $this->model
                ->withJoin(['user'], 'left')
                ->where($where)
                ->count();
            $list      = $this->model
                ->withJoin(['user'], 'left')
                ->where($where)
                ->page($page, $limit)
                ->order($this->sort)
                ->select()
                ->toArray();
            $adminIds1 = array_column($list, 'approve_admin_id');
            $adminIds2 = array_column($list, 'pay_admin_id');
            $adminIds  = array_merge($adminIds1, $adminIds2);
            $admins    = SystemAdmin::where([['id', 'in', $adminIds]])->select();
            $admins    = array_column($admins->toArray(), 'username', 'id');
            foreach ($list as $key => &$value) {
                $value[ 'approve_time' ] = $value[ 'approve_time' ] ? date('Y-m-d H:i:s', $value[ 'approve_time' ]) : '';
                $value[ 'pay_time' ]     = $value[ 'pay_time' ] ? date('Y-m-d H:i:s', $value[ 'pay_time' ]) : '';
                $value[ 'approve_user' ] = $admins[ $value[ 'approve_admin_id' ] ] ?? '';
                $value[ 'pay_user' ]     = $admins[ $value[ 'pay_admin_id' ] ] ?? '';
            }
            $total  = $this->model
                ->withJoin(['user'], 'left')
                ->where($where)
                ->value('SUM(amount)');
            $newArr = ['type' => '', 'user' => ['name' => '总计', 'mobile' => ''], 'amount' => $total,'status'=>''];
            array_unshift($list, $newArr);
            $data = [
                'code'  => 0,
                'msg'   => '',
                'count' => $count,
                'data'  => $list,
            ];
            return json($data);
        }
        return $this->fetch();
    }
    
    
    /**
     * @NodeAnotation(title="确认打款")
     */
    public function submit_payment($id)
    {
        $row = $this->model
            ->where('id', $id)
            ->find()->toArray();
//        halt($row);
        empty($row) && $this->error('数据不存在');
        if ($this->request->isPost()) {
            $this->checkPostRequest();
            $adminId = session('admin.id');
            $row     = $this->model->whereIn('id', $id)->find();
            try {
                $save = $row->save(['status' => 2, 'pay_time' => time(), 'pay_admin_id' => $adminId]);
                //提现成功短信
                $code = 'SMS_460840209';
                if ( !empty($code)) {
                    $mobile = MallUser::where('id', $row[ 'user_id' ])->value('mobile');
                    $msg    = [];
                    SendMsg::sendMsg($mobile, $msg, $code);
                    
                }
            } catch (\Exception $e) {
                $this->error('打款状态修改失败');
            }
            $save ? $this->success('打款成功') : $this->error('打款失败');
        }
        $row[ 'mobile' ] = MallUser::where('id', $row[ 'user_id' ])->value('mobile');
        $row[ 'amount' ] = getPrice($row[ 'amount' ]);
        $this->assign('row', $row);
        return $this->fetch();
    }
    
    /**
     * @NodeAnotation(title="驳回打款")
     */
    public function refuse($id)
    {
        $row = $this->model
            ->where('id', $id)
            ->find()->toArray();
//        halt($row);
        empty($row) && $this->error('数据不存在');
        if ($this->request->isPost()) {
            if (empty(input('remark'))) {
                $this->error('驳回理由不能为空');
            }
            if (strlen(input('remark'))>30){
                $this->error('最多输入30个字');
            }
            $this->checkPostRequest();
            $adminId = session('admin.id');
            $row     = $this->model->whereIn('id', $id)->find();
            try {
                $save = $row->save(['status' => 10, 'remark' => input('remark'), 'pay_time' => time(), 'pay_admin_id' => $adminId]);
                //将余额退回
                $balance    = Db::table('ea_mall_user')->where('id', $row[ 'user_id' ])->value('balance');
                $amount     = $row[ 'amount' ];
                $surBalance = $balance + $amount;
                Db::table('ea_mall_user')->where('id', $row[ 'user_id' ])->update(['balance' => $surBalance]);
                \app\api\model\MallTradeRecordLog::addRecord($row[ 'user_id' ], 0, $amount, $surBalance, 6);
                //提现失败短信
                $code = 'SMS_460786198';
                if ( !empty($code)) {
                    $mobile = MallUser::where('id', $row[ 'user_id' ])->value('mobile');
                    $msg    = ['msg'=>input('remark')];
                    SendMsg::sendMsg($mobile, $msg, $code);
                    
                }
            } catch (\Exception $e) {
                $this->error('驳回失败');
            }
            $save ? $this->success('驳回成功') : $this->error('驳回失败');
        }
        $this->assign('id', input('id'));
        $this->assign('row', $row);
        return $this->fetch();
    }
}